package com.zhangh.summer.admin.support.boot.common;

import com.zhangh.summer.admin.support.utils.YmlUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.WebApplicationType;

import java.util.ArrayList;
import java.util.List;

@Slf4j
//@Data
public class ApplicationConfig {

    public static final String  APPLICATION_NAME="applicationName";

    //程序名称
    private  String applicationName;
    //是否使用mysql数据源
    private  boolean isMysqlDataSourceEnable=true;
    //是否使用web mvc config
    private  boolean isWebMvcConfigurationEnable=false;
    //是否使用redis
    private boolean isRedisEnable =true;
    //是否启用job
    private boolean isJobEnable=false;
    //是否支持跨域
    private boolean isCorsEnable=false;
    //服务启动类型 默认不启用web
    private WebApplicationType webApplicationType= WebApplicationType.SERVLET;

    //spring排除的 package
    private List<String> exclusionPackage=new ArrayList<String>();
    //spring排除的 class
    private List<Class> exclusionClass=new ArrayList<Class>();

    private static ApplicationConfig instance;

    private ApplicationConfig (){
        init();
    }

    public static synchronized ApplicationConfig config() {
        if (instance == null) {
            instance = new ApplicationConfig();
        }
        return instance;
    }

    private  void init(){
        //设置app name
        applicationName= YmlUtil.get("spring.application.name");
        if (StringUtils.isBlank(applicationName)){
            System.err.println("no application.name config bootstrap.yml");
            throw new RuntimeException("no application.name config in bootstrap.yml");
        }
        System.setProperty(APPLICATION_NAME,applicationName);//设置环境变量，为log4j使用
    }

    /**
     * 获得程序名称
     * @return
     */
    public  String getApplicationName() {
        return this.applicationName;
    }

    /**
     * 设置程序名称
     * @param applicationName
     */
    public  ApplicationConfig setApplicationName(String applicationName) {
        this.applicationName = applicationName;
        return config();
    }
    /**
     * 是否使用mysql数据源
     * @return
     */
    public  boolean isMysqlDataSourceEnable() {
        return this.isMysqlDataSourceEnable;
    }

    /**
     * 设置是否使用mysql数据源
     * true 使用
     * false 不使用
     * @param mysqlDataSourceEnable
     */
    public  ApplicationConfig setMysqlDataSourceEnable(boolean mysqlDataSourceEnable) {
        this.isMysqlDataSourceEnable = mysqlDataSourceEnable;
        return config();
    }

    /**
     * 是否使用webMvc配置
     * @return
     */
    public  boolean isWebMvcConfigurationEnable() {
        return this.isWebMvcConfigurationEnable;
    }

    public  boolean isRedisEnable() {
        return this.isRedisEnable;
    }

    public ApplicationConfig setRedisEnable(boolean isRedisEnable){
        this.isRedisEnable=isRedisEnable;
        return config();
    }

    public  boolean isJobEnable() {
        return this.isJobEnable;
    }

    public ApplicationConfig setJobEnable(boolean isJobEnable){
        this.isJobEnable=isJobEnable;
        return config();
    }

    /**
     * 设置是否使用webMvc配置
     * true 使用
     * false 不使用
     * @param webMvcConfigurationEnable 是否使用mvc配置
     */
    public  ApplicationConfig setWebMvcConfigurationEnable(boolean webMvcConfigurationEnable) {
        this.isWebMvcConfigurationEnable = webMvcConfigurationEnable;
        if (webMvcConfigurationEnable) {
            this.webApplicationType = WebApplicationType.SERVLET;
        }else{
            this.webApplicationType = WebApplicationType.NONE;
        }
        return config();
    }

    /**
     * 排除扫描的包
     * @param packages
     * @return
     */
    public ApplicationConfig exclusionScan(String... packages  ){
        for (String pack:packages) {
            this.exclusionPackage.add(pack);
        }
        return config();
    }

    /**
     * 排除扫描的类
     * @param classes
     * @return
     */
    public ApplicationConfig exclusionScan(Class... classes  ){
        for (Class cls:classes) {
            this.exclusionClass.add(cls);
        }
        return config();
    }

    public boolean isCorsEnable(){ return  isCorsEnable;}

    /**
     * 是否开启跨域
     * @param isCorsEnable 是否跨域
     * @return  ApplicationConfig
     */
    public ApplicationConfig   setCorsEnable(boolean isCorsEnable) {
        this.isCorsEnable = isCorsEnable;
        return config();
    }

    /**
     * 设置程序类型
     * @param webApplicationType
     * @return
     */
    public ApplicationConfig  setWebApplicationType(WebApplicationType webApplicationType){
        this.webApplicationType=webApplicationType;
        return config();
    }

    public WebApplicationType getWebApplicationType(){
        return this.webApplicationType;
    }

    public List<String> getExclusionPackage() {
        return exclusionPackage;
    }

    public List<Class> getExclusionClass() {
        return exclusionClass;
    }
}
